import ctypes
import sys
import qdarkstyle
from PyQt5.QtCore import QUrl, QCoreApplication
from PyQt5.QtGui import QDesktopServices, QIcon
from src.Log_UI import Ui_Dialog
from src.user.UserUI_win import UserUi_Win
from src.admin.AdminUI_win import AdminUI_Win
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from src.Regester_win import Regester_Win
import mysql.connector


class Log_Win(QtWidgets.QWidget,Ui_Dialog):
    def __init__(self, parent=None,UserLevel = "admin"):
        super(Log_Win, self).__init__(parent=parent)
        #ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('')
        self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        #self.setWindowIcon(icon)
        self.setupUi(self)
        self.User_Level = UserLevel
        self.LoginSignal=False
        try:
            self.mydb = mysql.connector.connect(host="localhost", port=3306, user="Useless", passwd="123456", database="test")
        except:
            print("error")
        self.UserUI_form=UserUi_Win(self.mydb)
        self.AdminUI_form=AdminUI_Win(self.mydb)
        self.Reg_form=Regester_Win()

        self.Reg_form.setWindowTitle("注册界面")
        self.UserUI_form.setWindowTitle("会员界面")
        self.AdminUI_form.setWindowTitle("管理员界面")

        self.UserUI_form.pushButton_9.clicked.connect(self.myLogOut_U)
        self.AdminUI_form.pushButton_6.clicked.connect(self.myLogOut_A)


    def myRegister(self):
        self.Reg_form.show()

    def myLogin(self):
        User_Name=self.lineEdit.text()
        User_Password=self.lineEdit_2.text()
        try:
            self.mydb=mysql.connector.connect(host="localhost", port=3306, user=User_Name, passwd=User_Password, database="test")
        except Exception as a:
            QMessageBox.critical(self, '错误', '用户名或密码错误')
        else:
            s = "\'" + User_Name + "\'"
            myselect= "select * from 会员表 where 会员号 =" +s
            mycursor = self.mydb.cursor()
            mycursor.execute(myselect)
            result = mycursor.fetchall()
            self.UserUI_form.getMydb(self.mydb)
            self.AdminUI_form.getMydb(self.mydb)
            if self.User_Level == "consumer":
                if len(result)==0:
                    QMessageBox.critical(self, '错误', '用户名或密码错误')
                else:
                    self.UserUI_form.User_Name=User_Name
                    self.UserUI_form.show()
                    s1= "\'" + User_Name + "\'"
                    myselect1="SELECT 玩具个体表.`玩具编号`,玩具种类表.`种类描述` " \
                              "FROM 会员偏好表,玩具种类表,玩具个体表 " \
                              "WHERE 会员偏好表.`玩具种类号`=玩具种类表.`玩具种类号` AND 玩具种类表.`玩具种类号`=玩具个体表.`玩具种类号` AND 会员号 ="+s1
                    mycursor1=self.mydb.cursor()
                    mycursor1.execute(myselect1)
                    result1=mycursor1.fetchall()

                    self.UserUI_form.label_6.setText(result[0][1])
                    self.UserUI_form.label_7.setText(str(result[0][0]))
                    self.UserUI_form.label_8.setText(str(result[0][7]))
                    self.UserUI_form.label_9.setText(str(result[0][6]))
                    self.UserUI_form.label_16.setText(result1[0][1])
                    self.UserUI_form.label_20.setText(str(result1[0][0]))
                    self.UserUI_form.label_17.setText("暂无其它推荐")
                    self.UserUI_form.label_21.setText("无")
                    self.UserUI_form.label_22.setText("无")
                    self.UserUI_form.label_18.setText("暂无其它推荐")
                    self.UserUI_form.label_23.setText("玩具编号")
                    self.hide()
            else:
                if len(result)==0:
                    self.AdminUI_form.show()
                    self.hide()
                else:
                    QMessageBox.critical(self, '错误', '用户名或密码错误')


    def myExplain(self):
        return 0
    def myChange(self):
        if self.comboBox.currentText() == '玩具管理员':
            self.User_Level = "admin"
        else:
            self.User_Level = "consumer"
    def myLogOut_U(self):
        self.show()
        self.UserUI_form.hide()

    def myLogOut_A(self):
        self.show()
        self.AdminUI_form.hide()